from fastapi import HTTPException, status
from typing import Optional, Dict, Any

class ErrorCode:
    # 通用错误 (1000-1999)
    INTERNAL_ERROR = 1000  # 内部错误
    INVALID_PARAMS = 1001  # 无效的参数
    
    # 认证相关错误 (2000-2999)
    AUTH_FAILED = 2000  # 认证失败
    INVALID_TOKEN = 2001  # 无效的令牌
    TOKEN_EXPIRED = 2002  # 令牌已过期
    INVALID_REFRESH_TOKEN = 2003  # 无效的刷新令牌
    TOKEN_MISSING_INFO = 2004  # 令牌缺少必要信息
    
    # 用户相关错误 (3000-3999)
    USER_NOT_FOUND = 3000  # 用户不存在
    USER_ALREADY_EXISTS = 3001  # 用户已存在
    USER_INACTIVE = 3002  # 用户未激活
    INVALID_USERNAME = 3003  # 无效的用户名
    INVALID_PASSWORD = 3004  # 无效的密码
    INVALID_PHONE = 3005  # 无效的手机号
    PHONE_ALREADY_EXISTS = 3006  # 手机号已被使用
    PASSWORD_TOO_WEAK = 3007  # 密码强度不足
    
    # 权限相关错误 (4000-4999)
    PERMISSION_DENIED = 4000  # 权限不足
    NOT_SUPERUSER = 4001  # 需要超级用户权限

class APIException(HTTPException):
    def __init__(
        self,
        error_code: int,
        detail: str,
        status_code: int = status.HTTP_400_BAD_REQUEST,
        headers: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            status_code=status_code,
            detail={
                "code": error_code,
                "message": detail
            },
            headers=headers
        )

# 认证相关异常
class AuthenticationError(APIException):
    def __init__(self, error_code: int, detail: str):
        super().__init__(
            error_code=error_code,
            detail=detail,
            status_code=status.HTTP_401_UNAUTHORIZED,
            headers={"WWW-Authenticate": "Bearer"}
        )

class InvalidCredentialsError(AuthenticationError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.AUTH_FAILED,
            detail="用户名或密码错误"
        )

class TokenError(AuthenticationError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.INVALID_TOKEN,
            detail="无效的访问令牌"
        )

class TokenExpiredError(AuthenticationError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.TOKEN_EXPIRED,
            detail="访问令牌已过期"
        )

class RefreshTokenError(AuthenticationError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.INVALID_REFRESH_TOKEN,
            detail="无效的刷新令牌"
        )

# 用户相关异常
class UserError(APIException):
    def __init__(self, error_code: int, detail: str):
        super().__init__(
            error_code=error_code,
            detail=detail,
            status_code=status.HTTP_400_BAD_REQUEST
        )

class UserNotFoundError(UserError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.USER_NOT_FOUND,
            detail="用户不存在"
        )

class UserAlreadyExistsError(UserError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.USER_ALREADY_EXISTS,
            detail="用户已存在"
        )

class UserInactiveError(UserError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.USER_INACTIVE,
            detail="用户未激活"
        )

class InvalidPhoneError(UserError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.INVALID_PHONE,
            detail="无效的手机号"
        )

class PhoneAlreadyExistsError(UserError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.PHONE_ALREADY_EXISTS,
            detail="手机号已被使用"
        )

class WeakPasswordError(UserError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.PASSWORD_TOO_WEAK,
            detail="密码强度不足，密码必须包含字母和数字，且长度至少为8位"
        )

# 权限相关异常
class PermissionError(APIException):
    def __init__(self, error_code: int, detail: str):
        super().__init__(
            error_code=error_code,
            detail=detail,
            status_code=status.HTTP_403_FORBIDDEN
        )

class NotSuperuserError(PermissionError):
    def __init__(self):
        super().__init__(
            error_code=ErrorCode.NOT_SUPERUSER,
            detail="权限不足，需要超级用户权限"
        )